package com.fr.web.core.service;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.file.CacheManager;
import com.fr.report.PageSet;
import com.fr.report.ResultWorkBook;
import com.fr.report.core.cal.LayerPageReport;
import com.fr.report.core.cal.LayerReport;
import com.fr.report.io.AppExporter;
import com.fr.report.io.CSVExporter;
import com.fr.report.io.ExcelExporter;
import com.fr.report.io.PDFExporter;
import com.fr.report.io.SVGExporter;
import com.fr.report.io.TextExporter;
import com.fr.report.web.EmailManager;
import com.fr.util.Utils;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.WebUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EmailService extends NoOPService
{
  private static EmailService EMAIL_SERVICE = new EmailService();

  public static EmailService getInstance()
  {
    return EMAIL_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.equals("email");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.println("<html><head>");
    localPrintWriter.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">");
    localPrintWriter.println("<link rel='stylesheet' href='" + WebUtils.createServletURL(paramHttpServletRequest) + "?op=resource&resource=/com/fr/web/core/reportPage.css' type='text/css'>");
    localPrintWriter.println("<title>" + Inter.getLocText("Email") + "</title>");
    localPrintWriter.println("</head><body>");
    SessionIDInfor localSessionIDInfor = SessionDealWith.getSessionIDInfor(paramString);
    if (localSessionIDInfor == null)
      return;
    String str1 = "";
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "format");
    if (str2.equalsIgnoreCase("PDF"))
    {
      str1 = "pdf";
    }
    else if (str2.equalsIgnoreCase("Excel"))
    {
      localObject1 = localSessionIDInfor.getWorkBook2Show();
      i = 0;
      for (int j = 0; j < ((ResultWorkBook)localObject1).getReportCount(); ++j)
        if ((((ResultWorkBook)localObject1).getResultReport(j) instanceof LayerReport) || (((ResultWorkBook)localObject1).getResultReport(j) instanceof LayerPageReport))
        {
          i = 1;
          break;
        }
      str1 = (i != 0) ? "zip" : "xls";
    }
    else if (str2.equalsIgnoreCase("SVG"))
    {
      str1 = "svg";
    }
    else if (str2.equalsIgnoreCase("HTML"))
    {
      str1 = "zip";
    }
    else if (str2.equalsIgnoreCase("CSV"))
    {
      str1 = "csv";
    }
    else if (str2.equalsIgnoreCase("Text"))
    {
      str1 = "txt";
    }
    Object localObject1 = new File(FRContext.getCacheManager().getCacheDirectory(), "_" + System.currentTimeMillis() + File.separator + "report." + str1);
    for (int i = 0; ((File)localObject1).exists(); ++i)
      localObject1 = new File(FRContext.getCacheManager().getCacheDirectory(), "_" + System.currentTimeMillis() + File.separator + "report_" + i + "." + str1);
    Utils.mkdirs(((File)localObject1).getParentFile());
    FileOutputStream localFileOutputStream = new FileOutputStream((File)localObject1);
    Object localObject2 = null;
    if (str2.equalsIgnoreCase("PDF"))
      localObject2 = new PDFExporter();
    else if (str2.equalsIgnoreCase("Excel"))
      localObject2 = new ExcelExporter(null);
    else if (str2.equalsIgnoreCase("SVG"))
      localObject2 = new SVGExporter();
    else if (str2.equalsIgnoreCase("CSV"))
      localObject2 = new CSVExporter();
    else if (str2.equalsIgnoreCase("Text"))
      localObject2 = new TextExporter();
    try
    {
      if (localObject2 != null)
        if (localObject2 instanceof ExcelExporter)
        {
          ((ExcelExporter)localObject2).export(localFileOutputStream, localSessionIDInfor.getWorkBook2Show(), true);
        }
        else
        {
          PageSet localPageSet = localSessionIDInfor.getPrintPreviewPageSet4Traversing();
          ((AppExporter)localObject2).export(localFileOutputStream, localPageSet);
        }
      ((File)localObject1).deleteOnExit();
      ((File)localObject1).getParentFile().deleteOnExit();
    }
    catch (Exception localException1)
    {
      FRContext.getLogger().log(Level.WARNING, localException1.getMessage(), localException1);
      localPrintWriter.println("<form><input type=\"button\" value=\"Back\" onClick=\"history.back();\">");
      localPrintWriter.println("<input type=\"button\" value=\"Close\" onClick=\"window.close();\"></form>");
      localPrintWriter.println("<pre>");
      localException1.printStackTrace(localPrintWriter);
      localPrintWriter.println("</pre>");
      localPrintWriter.println("<br>");
      localPrintWriter.println("</body></html>");
      localPrintWriter.flush();
      localPrintWriter.close();
      return;
    }
    try
    {
      EmailManager localEmailManager = localSessionIDInfor.getEmailManager();
      if (localEmailManager == null)
        throw new Exception("Email Config needed.");
      localEmailManager.send(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "to"), WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "cc"), WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "bcc"), WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "from"), WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "subject"), WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "message"), localObject1);
      localPrintWriter.println("Send successfully...<br>");
    }
    catch (Exception localException2)
    {
      FRContext.getLogger().log(Level.WARNING, localException2.getMessage(), localException2);
      localPrintWriter.println("<form><input type=\"button\" value=\"Back\" onClick=\"history.back();\">");
      localPrintWriter.println("<input type=\"button\" value=\"Close\" onClick=\"window.close();\"></form>");
      localPrintWriter.println("<p>");
      localPrintWriter.println("<pre>");
      localPrintWriter.println("Send failed...");
      localException2.printStackTrace(localPrintWriter);
      localPrintWriter.println("</pre>");
      localPrintWriter.println("</body></html>");
      localPrintWriter.flush();
      localPrintWriter.close();
      return;
    }
    localPrintWriter.println("<form><input type=\"button\" value=\"Back\" onClick=\"history.back();\">");
    localPrintWriter.println("<input type=\"button\" value=\"Close\" onClick=\"window.close();\"></form>");
    localPrintWriter.println("</body></html>");
    localPrintWriter.flush();
    localPrintWriter.close();
  }
}